package com.hadoop.project.mvc.service.impl;

import com.hadoop.project.entity.po.Demo;
import com.hadoop.project.entity.query.DemoQuery;
import com.hadoop.project.entity.query.SimplePage;
import com.hadoop.project.entity.vo.PaginationResultVO;
import com.hadoop.project.entity.enums.PageSize;
import com.hadoop.project.mvc.mappers.DemoMapper;
import com.hadoop.project.mvc.service.DemoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: Service Impl
 * @author:alexmaodali
 * @date:2025/4/2
 */
@Service("demoService")
public class DemoServiceImpl implements DemoService {

	@Resource
	private DemoMapper<Demo,DemoQuery> demoMapper;

	/**
	 * 根据条件查询列表
	 */
	public List<Demo> findListByQuery(DemoQuery query) {
		return this.demoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByQuery(DemoQuery query) {
		return this.demoMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<Demo> findListByPage(DemoQuery query) {
		Integer count = this.findCountByQuery(query);
		Integer pageSize = query.getPageSize() == null ? PageSize.SIZE15.getSize() : query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(), count, pageSize);
		query.setSimplePage(page);
		List<Demo> list = new ArrayList<>();
		PaginationResultVO<Demo> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}


	/**
	 * 新增
	 */
	public Integer add(Demo bean) {
		return this.demoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<Demo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			 return 0;
		}
		return this.demoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或修改
	 */
	public Integer addOrUpdateBatch(List<Demo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			 return 0;
		}
		return this.demoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据Id查询
	 */
	public Demo getDemoById(Integer id) {
		return this.demoMapper.selectById(id);
	}

	/**
	 * 根据Id更新
	 */
	public Integer updateDemoById(Demo bean,Integer id) {
		return this.demoMapper.updateById(bean,id);
	}

	/**
	 * 根据Id删除
	 */
	public Integer deleteDemoById(Integer id) {
		return this.demoMapper.deleteById(id);
	}


}
